<?php
/*
 * description：
 * author：wh
 * email：
 * createTime：{2020/2/21} {16:59} 
 */

namespace libraries\systemlogs;

use libraries\DbCacheUtility;
use libraries\Tools;
use think\Db;

/**
 * 日志底层
 * Class BaseLog
 * @package libraries\systemlogs
 */
class BaseLog
{
    protected $self_table = '';//数据表
    protected $identity = '';
    function __construct()
    {
        $this->identity = Tools::getMicrosecond();//一个流程的标识
    }

    function setLog($title='', $content){
        $obj = Db::table($this->self_table);
        return $obj->insertGetId([
            'title'=>$title,
            'identity'=>$this->identity,
            'content'=>encodeJson($content),
        ]);
    }

    function setInLog($title='', $content){
        $obj = Db::table($this->self_table);
        return $obj->insertGetId([
            'title'=>'IN|'.$title,
            'identity'=>$this->identity,
            'content'=>encodeJson($content),
        ]);
    }

    function setOutLog($title='', $content){
        $obj = Db::table($this->self_table);
        return $obj->insertGetId([
            'title'=>'OUT|'.$title,
            'identity'=>$this->identity,
            'content'=>encodeJson($content),
        ]);
    }

    function getLogById(int $id){
        $obj = Db::table($this->self_table)->where(['id'=>$id]);
        $data = DbCacheUtility::getOne($obj);
        return $data;
    }
}